using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace demo
{
    class SyncFormTestToForm1
    {
        static void Main(string[] args)
        {
            Console.WriteLine("========================================");
            Console.WriteLine("环境同步工具 - FormTest -> Form1");
            Console.WriteLine("========================================");
            Console.WriteLine();

            try
            {
                // 获取当前目录
                string currentDir = Directory.GetCurrentDirectory();
                
                // 如果在bin目录中，向上找到项目根目录
                string projectDir = currentDir;
                if (currentDir.Contains("bin"))
                {
                    int binIndex = currentDir.IndexOf("\\bin\\");
                    if (binIndex > 0)
                    {
                        projectDir = currentDir.Substring(0, binIndex);
                    }
                }

                Console.WriteLine(string.Format("项目目录: {0}", projectDir));
                Console.WriteLine();

                // 定义文件映射
                Dictionary<string, string> fileMappings = new Dictionary<string, string>();
                fileMappings.Add("FormTest.cs", "Form1.cs");
                fileMappings.Add("FormTest.Designer.cs", "Form1.Designer.cs");
                fileMappings.Add("FormTest.resx", "Form1.resx");

                // 定义内容替换
                Dictionary<string, string> contentMappings = new Dictionary<string, string>();
                contentMappings.Add("FormTest", "Form1");
                contentMappings.Add("测试环境", "");
                contentMappings.Add(@"\[测试环境\]", "");

                // 创建备份目录
                string backupDir = Path.Combine(projectDir, string.Format("Backup_{0:yyyyMMdd_HHmmss}", DateTime.Now));
                Directory.CreateDirectory(backupDir);
                Console.WriteLine(string.Format("备份目录: {0}", backupDir));
                Console.WriteLine();

                // 同步文件
                foreach (KeyValuePair<string, string> mapping in fileMappings)
                {
                    string sourceFile = mapping.Key;
                    string targetFile = mapping.Value;
                    string sourcePath = Path.Combine(projectDir, sourceFile);
                    string targetPath = Path.Combine(projectDir, targetFile);

                    Console.WriteLine(string.Format("处理文件: {0} -> {1}", sourceFile, targetFile));

                    // 检查源文件是否存在
                    if (!File.Exists(sourcePath))
                    {
                        Console.WriteLine(string.Format("  警告: 源文件不存在: {0}", sourcePath));
                        continue;
                    }

                    // 备份目标文件（如果存在）
                    if (File.Exists(targetPath))
                    {
                        string backupPath = Path.Combine(backupDir, targetFile);
                        File.Copy(targetPath, backupPath, true);
                        Console.WriteLine(string.Format("  已备份: {0}", targetFile));
                    }

                    // 读取源文件内容
                    string content = File.ReadAllText(sourcePath, Encoding.UTF8);

                    // 应用内容替换
                    foreach (KeyValuePair<string, string> contentMapping in contentMappings)
                    {
                        content = Regex.Replace(content, contentMapping.Key, contentMapping.Value);
                    }

                    // 写入目标文件
                    File.WriteAllText(targetPath, content, Encoding.UTF8);
                    Console.WriteLine(string.Format("  已同步: {0}", targetFile));
                    Console.WriteLine();
                }

                Console.WriteLine("同步完成！");
                Console.WriteLine();
                Console.WriteLine("建议:");
                Console.WriteLine("1. 重新编译项目");
                Console.WriteLine("2. 将App.config中的Environment设置为Production");
                Console.WriteLine("3. 测试Form1功能是否正常");
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("同步过程中出错: {0}", ex.Message));
                Console.WriteLine(ex.StackTrace);
            }

            Console.WriteLine();
            Console.WriteLine("按任意键退出...");
            Console.ReadKey();
        }
    }
}